package com.ssy.lingxi.search.serviceImpl;

import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.search.entity.OftenSelect;
import com.ssy.lingxi.search.repository.OftenSelectRepository;
import com.ssy.lingxi.search.service.IOftenSelectService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商城实现类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/22
 */
@Service
public class OftenSelectServiceImpl implements IOftenSelectService {

    @Resource
    private OftenSelectRepository oftenSelectRepository;

    /**
     * 查询常用筛选列表
     * @param pageVO
     */
    @Override
    public Page<OftenSelect> getOftenSelectList(UserLoginCacheDTO sysUser, PageVO pageVO){
        Long userId = sysUser.getUserId();
        Pageable page = PageRequest.of(pageVO.getCurrent() - 1, pageVO.getPageSize());
        return oftenSelectRepository.findByUserId(userId, page);
    }

    /**
     * 查询常用筛选
     * @param id
     * @return OftenSelect
     */
    @Override
    public OftenSelect getOftenSelect(Long id){
        return oftenSelectRepository.findById(id).orElse(null);
    }

    /**
     * 添加/修改常用筛选
     * @param oftenSelect
     * @return
     */
    @Override
    public Long saveOrUpdateOftenSelect(UserLoginCacheDTO sysUser, OftenSelect oftenSelect){
        Long userId = sysUser.getUserId();
        Long id = oftenSelect.getId();
        String name = oftenSelect.getName();
        List<OftenSelect> old_oftenSelect;
        //判断是新增还是修改
        if(id != null && id > 0){
            //验证数据库中是否存在该数据
            OftenSelect current_oftenSelect = oftenSelectRepository.findById(id).orElse(null);
            if(current_oftenSelect != null){
                old_oftenSelect = oftenSelectRepository.findByUserIdAndNameAndIdNot(userId, name, id);
                if(!old_oftenSelect.isEmpty()){
                    throw new BusinessException(ResponseCode.PRODUCT_OFTEN_SELECT_NOT_EXIST);
                }
            }else{
                throw new BusinessException(ResponseCode.PRODUCT_OFTEN_SELECT_NOT_EXIST);
            }
        }else{
            old_oftenSelect = oftenSelectRepository.findByUserIdAndName(userId, name);
            if(!old_oftenSelect.isEmpty()){
                throw new BusinessException(ResponseCode.PRODUCT_OFTEN_SELECT_NAME_EXIST);
            }
        }
        //数据库持久化对象
        oftenSelect.setUserId(sysUser.getUserId());
        oftenSelect.setUserName(sysUser.getUserName());
        oftenSelectRepository.saveAndFlush(oftenSelect);
        return oftenSelect.getId();
    }

    /**
     * 删除常用筛选
     * @param id
     * @return
     */
    @Override
    public String deleteOftenSelect(Long id) {
        //验证数据库中是否存在该数据
        OftenSelect oftenSelect = oftenSelectRepository.findById(id).orElse(null);
        if(oftenSelect != null){
            oftenSelectRepository.deleteById(id);
            return ResponseCode.SUCCESS.getMessage();
        }
        throw new BusinessException(ResponseCode.PRODUCT_OFTEN_SELECT_NOT_EXIST);
    }

}

